package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;

import mappers.RoomMapper;

import domains.Room;

public class AddRoomGUI {
	private JFrame addRoomFrame;
	private JTextField roomNameTF, roomSizeTF;
	private JLabel roomNameLbl, roomSizeLbl;
	private JPanel p, p1, p2;
	private JButton addBtn, cancelBtn; 
	private JCheckBox smartBoardBtn, projectorBtn;
	
	public AddRoomGUI() {
		addRoomFrame = new JFrame();
		addRoomFrame.setTitle("Add Room");
		addRoomFrame.setLayout(new BorderLayout());
		addRoomFrame.setSize(500, 400);		
		
		roomNameLbl = new JLabel("Room name:");
		roomNameTF = new JTextField(15);
		
		roomSizeLbl = new JLabel("Size:");
		roomSizeTF = new JTextField(4);
			
		p1 = new JPanel(new FlowLayout());
		
		p = new JPanel(new GridLayout(3,2));
		p.add(roomNameLbl);
		p.add(roomNameTF);
		p.add(roomSizeLbl);
		p.add(roomSizeTF);
		p.add(p1);
		
		smartBoardBtn = new JCheckBox("Smart Board");
		smartBoardBtn.setMnemonic(KeyEvent.VK_S);
		
		projectorBtn = new JCheckBox("Projector");
		projectorBtn.setMnemonic(KeyEvent.VK_P);
		
		p1.add(smartBoardBtn);
		p1.add(projectorBtn);
			
		p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		
		cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addRoomFrame.setVisible(false);
			}		
		});
		
		addBtn = new JButton("Add");
		addBtn.addActionListener(new ActionListener() {
			int roomSize;
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					roomSize = Integer.parseInt(roomSizeTF.getText());
					Room r = new Room(roomNameTF.getText(), roomSize);
					RoomMapper rMapper = new RoomMapper();
					rMapper.addRoom(r);
				} catch(NumberFormatException e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, 
							"Ooops! The size should be a number");
				}		
			}			
		});
		
		p2.add(addBtn);
		p2.add(cancelBtn);


		addRoomFrame.add(p, BorderLayout.WEST);
		addRoomFrame.add(p2, BorderLayout.SOUTH);
		addRoomFrame.pack();
		addRoomFrame.setVisible(true);
	}
}
